package com.buddies.lottery.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class PopupWindowUtils {

    private static PopupWindowUtils instance;

    private PopupWindow mPopupWindow;

    // 私有化构造方法，变成单例模式
    private PopupWindowUtils() {

    }

    // 对外提供一个该类的实例，考虑多线程问题，进行同步操作
    public static PopupWindowUtils getInstance() {
        if (instance == null) {
            synchronized (PopupWindowUtils.class) {
                if (instance == null) {
                    instance = new PopupWindowUtils();
                }
            }
        }
        return instance;
    }

    /**
     * @param context     activity
     * @param width       width
     * @param height      height
     * @param contentView 传入内容的view
     * @return
     */
    public PopupWindowUtils makePopupWindow(Context context, int width, int height, View contentView, int color) {
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        mPopupWindow = new PopupWindow(context);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(color));
        // 设置PopupWindow的大小（宽度和高度）
        mPopupWindow.setWidth(width);
        mPopupWindow.setHeight(height);
        // 设置PopupWindow的内容view
        mPopupWindow.setContentView(contentView);
        mPopupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
        mPopupWindow.setTouchable(true); // 设置PopupWindow可触摸
        mPopupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        return instance;
    }

    /**
     * @param activity 此处必须为Activity的实例
     * @param view     显示在该控件之下
     * @param xOff     距离view的x轴偏移量
     * @param yOff     距离view的y轴偏移量
     * @param anim     弹出及消失动画
     * @return
     */
    public PopupWindow showLocationWithAnimation(final Activity activity, View view, int xOff, int yOff, int anim) {
        // 弹出动画
        mPopupWindow.setAnimationStyle(anim);

        // 弹出PopupWindow时让后面的界面变暗
        final Window window = activity.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.alpha = 0.7f;
        window.setAttributes(params);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        mPopupWindow.showAsDropDown(view, xOff, yOff);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // PopupWindow消失后让后面的界面变亮
                WindowManager.LayoutParams params = window.getAttributes();
                params.alpha = 1.0f;
                window.setAttributes(params);
                window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

                if (mListener != null) {
                    mListener.dissmiss();
                }
            }
        });

        return mPopupWindow;
    }

    private interface OnDissmissListener {

        void dissmiss();

    }

    private OnDissmissListener mListener;

    public void setOnDissmissListener(OnDissmissListener listener) {
        mListener = listener;
    }

    public void dismiss() {
        if (isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    public boolean isShowing() {
        return mPopupWindow != null && mPopupWindow.isShowing();
    }

}